'use strict';
//
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	//获取数据库对象
	const db = uniCloud.database();
	//获取表的对象
	const tb = db.collection("company");
	//根据类型进行操作
	switch (event.type) {
		//查找所有公司
		case 'companys': {
			let res = await tb.get();
			return res;
		};
		//根据公司id查找对应公司
		case "selectById": {
		let {_id}=event;
		//查找
		let data=await tb.where({
			_id:_id
		}).get()
		return {status:200,data:data}
		};
		//根据id查找公司及它所招聘的岗位(联表)
		case "selectCompanyById": {
		let {company_id}=event;
		//查找
		let data=await tb.aggregate().match({
			_id:company_id
		}).lookup({
			from:'job',
			localField:'_id',
			foreignField:'c_id',
			as:'jobsList'
		}).end()
		return {status:200,data:data}
		};
		//添加公司
		case "add": {
			//提取参数
			let {type} = event;
			let res = await tb.add({
				type
			});
			return res;
		};
		//根据id删除公司
		case "deleteById": {
			let _id = event.id;
			let res = await tb.doc(_id).remove()
			return res;
		}

	}
};
